﻿using System.Web.Mvc;
using IterationLife.Extensions.MvcApi;
using IterationLife.Mobile.AppServices.Services;
using IterationLife.Mobile.AppServices.Services.System;
using IterationLife.System.ExternalIdentity;

namespace IterationLife.Mobile.CampusFootball.Student.ApiGateway.Controllers
{
    //[MvcApiAuthorize]
    /// <summary>
    /// 安全认证控制器
    /// </summary>
    public class AuthenticationController : MvcApiController
    {
        private readonly IAuthenticationService _authenticationService;

        public AuthenticationController(IAuthenticationService authenticationService)
        {
            _authenticationService = authenticationService;
        }

        /// <summary>
        /// 发送注册验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SendRegisterCode(string phone)
        {
            _authenticationService.SendRegisterCode(phone);
            return Ok();
        }

        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Register(string username, string password, string phone = null, string verifyCode = null)
        {
            var token = _authenticationService.Register(username, password, phone, verifyCode);
            return Ok(data: token, message: "注册成功");
        }

        /// <summary>
        /// 账号登录
        /// </summary>
        [HttpPost]
        public ActionResult Login(string account, string password)
        {
            var result = _authenticationService.Login(account, password);
            return Ok(data: result, message: "登录成功");
        }
        /// <summary>
        /// 发送找回密码验证码
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SendGetBackPasswordCode(string phone)
        {
            _authenticationService.SendGetBackPasswordCode(phone);
            return Ok();
        }
        /// <summary>
        /// 找回密码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="newPassword"></param>
        /// <param name="verifyCode"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult GetBackPassword(string phone, string newPassword, string verifyCode)
        {
            _authenticationService.GetBackPassword(phone, newPassword, verifyCode);
            return Ok();
        }

        public ActionResult SendBindingPhoneCode(string phone)
        {
            return null;
        }

        public ActionResult BindingPhone(string phone, string verifyCode)
        {
            return null;
        }

        public ActionResult SendUnbindingPhoneCode(string phone)
        {
            return null;
        }

        public ActionResult UnbindingPhone(string phone, string verifyCode)
        {
            return null;
        }

        /// <summary>
        /// 微信登录
        /// </summary>
        /// <param name="authorizationCode"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public ActionResult WechatLogin(string authorizationCode = null, string accessToken = null)
        {
            return ExternalLogin(ExternalUserProviderConstants.Wechat, authorizationCode, accessToken);
        }

        /// <summary>
        /// 第三方登录
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="authorizationCode"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        private ActionResult ExternalLogin(string provider, string authorizationCode = null, string accessToken = null)
        {
            var result = _authenticationService.ExternalLogin(provider, authorizationCode, accessToken);
            return Ok(data: result, message: "登录成功");
        }
    }
}